SWFUploadHander = {
	fileQueued : function(file) {
		this.swfu.debug("fileQueued:" + file.id);
		var filetype = (file.type.substr(1)).toUpperCase();
		var data = [];
		data.push([file.id, '未上传', file.name, file.size, filetype]);
		this.uploadGrid.store.loadData(data, true);
		this.uploadAction[1].enable();
		this.uploadAction[2].enable();
		this.uploadAction[3].enable();
		this.updateGridStaeInfoBar();
	},
	fileQueuedError : function(fileObj, error_code, message) {
		this.swfu.debug("fileQueuedError");
		switch (error_code) {			
			case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT :
				this.fileValidMsg += "文件太大，文件名：" + fileObj.name + "，文件大小："+ Ext.util.Format.fileSize(fileObj.size) + ".\n"+ message + "<br/>";
				break;
			case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE :this.fileValidMsg += "不能选择空文件，文件名：" + fileObj.name + "，文件大小："+ fileObj.size + "./n" + message + "<br/>";
				break;
			case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE :this.fileValidMsg += "文件类型无效，文件名：" + fileObj.name + "，文件大小："+ fileObj.size + ".\n" + "允许文件类型为："+ this.SWFUploadConfig.file_types + ".\n" + smessage+ "<br/>";
				break;
			case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED :
				this.fileValidMsg += "选择了太多的文件" + "<br/>";
				break;
			default :
				this.fileValidMsg += "发生不可预料的错误，" + "错误编码: " + error_code+ ", 文件名: " + fileObj.name + ", 文件大小: "+ fileObj.size + ", Message: " + message + "<br/>";
				break;
		}
		return false;
	},
	fileDialogComplete : function() {
		this.swfu.debug("fileDialogComplete" );
		//if (this.swfu.getStats().files_queued == 0) {
			if (this.fileValidMsg != null && this.fileValidMsg != "") {
				this.msg(this.fileValidMsg);
				this.fileValidMsg="";
			}
		//}
	},
	uploadStart : function(file) {
		this.swfu.debug("uploadStart:" + file.id);
		var index = this.findData(file.id);
		if (index >= 0) {
			this.uploadGrid.store.getAt(index).set('state', '正在上传……');
		}
		this.progressBar.updateProgress(0, String.format(this.progressBarText,file.name, 0));
		return true;
	},
	uploadProgress : function(file, bytesloaded) {
		var percent = Math.ceil((bytesloaded / file.size) * 100);
		this.progressBar.updateProgress(percent / 100, String.format(this.progressBarText, file.name, percent));
	},
	uploadComplete : function(file) {
		this.swfu.debug("uploadComplete:" + file.id);
		if (this.swfu.getStats().files_queued === 0) {
			if (this.fileValidMsg != null && this.fileValidMsg != "") {
				this.progressBar.updateProgress(0, '');
				this.progressBar.updateText("上传失败了");
				this.msg(this.fileValidMsg);
			}
			this.progressBar.updateText("全部完成上传,请关闭窗口！");
			// this.uploadAction[2].enable();
			this.uploadAction[4].enable();
		} else {
			this.swfu.startUpload();
		}
	},
	uploadSuccess : function(file, server_data) {
		// Ext.Msg.alert("server_data", server_data);
		var index = this.findData(file.id);
		if (index >= 0) {
			this.uploadGrid.store.getAt(index).set('state', '已上传');
		}
	},
	uploadError : function(fileObj, error_code, message) {
		this.swfu.debug("uploadError:" + fileObj.id);
		var index = this.findData(fileObj.id);
		if (index >= 0) {
			this.uploadGrid.store.getAt(index).set('state', '上传失败');
		}
		switch (error_code) {
			case SWFUpload.UPLOAD_ERROR.HTTP_ERROR :this.fileValidMsg += "上传错误，请检查配置" + "错误编码: " + error_code+ ", 文件名: " + fileObj.name + ", 文件大小: " + fileObj.size+ ", Message: " + message + "<br/>";
				break;
			case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL :this.fileValidMsg += "缺少upload_url配置，" + "错误编码: " + error_code+ ", 文件名: " + fileObj.name + ", 文件大小: " + fileObj.size+ ", Message: " + message + "<br/>";
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED :this.fileValidMsg += "上传失败，" + "错误编码: " + error_code+ ", 文件名: " + fileObj.name + ", 文件大小: " + fileObj.size+ ", Message: " + message + "<br/>";
				break;
			case SWFUpload.UPLOAD_ERROR.IO_ERROR :
				this.fileValidMsg += "上传Server (IO) Error，" + "错误编码: "+ error_code + ", 文件名: " + fileObj.name + ", 文件大小: "+ fileObj.size + ", Message: " + message + "<br/>";
				break;
			case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR :
				this.fileValidMsg += "Security Error，" + "错误编码: " + error_code+ ", 文件名: " + fileObj.name + ", 文件大小: " + fileObj.size+ ", Message: " + message + "<br/>";
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED :this.fileValidMsg += "Upload limit exceeded，" + "错误编码: "+ error_code + ", 文件名: " + fileObj.name + ", 文件大小: "+ fileObj.size + ", Message: " + message + "<br/>";
				break;
			case SWFUpload.UPLOAD_ERROR.SPECIFIED_FILE_ID_NOT_FOUND :this.fileValidMsg += "File not found，" + "错误编码: " + error_code+ ", 文件名: " + fileObj.name + ", 文件大小: " + fileObj.size+ ", Message: " + message + "<br/>";
				break;
			case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED :this.fileValidMsg += "Failed Validation.  Upload skipped，"+ "错误编码: " + error_code + ", 文件名: " + fileObj.name+ ", 文件大小: " + fileObj.size + ", Message: " + message+ "<br/>";
				break;
			case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED :
				if (this.swfu.getStats().files_queued === 0) {
					this.fileValidMsg +='文件上传被取消'+ "<br/>";
				}
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED :
				this.fileValidMsg += "Stopped，" + "错误编码: " + error_code+ ", 文件名: " + fileObj.name + ", 文件大小: " + fileObj.size+ ", Message: " + message + "<br/>";
				break;
			default :
				this.fileValidMsg += "Unhandled Error:，" + "错误编码: "+ error_code + ", 文件名: " + fileObj.name + ", 文件大小: "+ fileObj.size + ", Message: " + message + "<br/>";
				break;
		}
		this.swfu.stopUpload();
	},
	uploadCancel : function(file, queuelength) {
		this.swfu.debug("uploadCancel:" + file.id);
		var index = this.findData(file.id);
		if (index >= 0) {
			this.uploadGrid.store.getAt(index).set('state', '取消上传');
		}
	},
	findData : function(id) {
		var rowindex = this.uploadGrid.store.find('id', id);
		return rowindex;
	}
}
Ext.apply(SWFUploadHander, {
	addFile : function() {
		this.swfu.selectFiles();
	},
	removeFile : function() {
		var grid = this.uploadGrid;
		var store = grid.store;
		var selection = grid.getSelectionModel().getSelections();
		for (var i = 0;i < selection.length; i++) {
			var rec = store.getById(selection[i].id);
			this.swfu.cancelUpload(rec.data.id);
			store.remove(rec);
		}
		this.updateGridStaeInfoBar();
		if (this.uploadGrid.store.getCount() == 0) {
			this.uploadGrid.store.removeAll();
			this.uploadAction[1].disable();
			this.uploadAction[2].disable();
			this.uploadAction[3].disable();
		}
	},
	clearFile : function() {
		var store = this.uploadGrid.store;
		var len = store.getCount();
		for (var i = 0;i < len; i++) {
			var rec = store.getAt(i);
			this.swfu.cancelUpload(rec.data.id);
		}
		store.removeAll();
		this.updateGridStaeInfoBar();
		this.progressBar.updateProgress(0, '');
		this.progressBar.updateText("");
		this.uploadAction[0].enable();
		this.uploadAction[1].disable();
		this.uploadAction[2].disable();
		this.uploadAction[3].disable();
	},
	uploadFile : function() {
		this.uploadAction[0].disable();
		this.uploadAction[1].disable();
		this.uploadAction[2].disable();
		this.uploadAction[3].disable();
		this.swfu.startUpload();
	},
	closeWin : function() {
		this.hide();
		this.fireEvent('close');
		this.destroy();
	},
	updateGridStaeInfoBar : function() {
		this.stateInfoBar.getEl().innerHTML = String.format(this.stateInfoBarText, this.SWFUploadConfig.file_upload_limit,Ext.util.Format.fileSize(this.SWFUploadConfig.file_size_limit* 1024), this.uploadGrid.store.getCount(),Ext.util.Format.fileSize(this.uploadGrid.store.sum('size')));
	},
	msg : function(msg) {
		Ext.Msg.alert("错误信息", msg);
	}
});
ExtSwfAbstractFileupload = Ext.extend(Ext.Window, SWFUploadHander);
ExtSWFUpload = Ext.extend(ExtSwfAbstractFileupload, {	
	constructor:function(config) {
		Ext.apply(this,config);
		this.progressBarText = '正在上传：{0}，{1}%完成';
		this.stateInfoBarText = '最大文件数：{0}, 单个文件最大为：{1}， 文件总数：{2}个, 大小：{3} ';
		this.fileValidMsg = "";
		this.progressBar = new Ext.ProgressBar({width:480});
		this.stateInfoBar = new Ext.Toolbar.TextItem("");

		this.uploadAction = [new Ext.Action( {
			text : '<span id="spanButtonPlaceHolder">增加</span>',
			disabled : false
			//handler : this.addFile.createDelegate(this)
		}), new Ext.Action( {
			text : '删除',
			disabled : false,
			handler : this.removeFile.createDelegate(this)
		}), new Ext.Action( {
			text : '清空',
			disabled : false,	
			handler : this.clearFile.createDelegate(this)
		}), new Ext.Action( {
			text : '上传',
			disabled : false,
			handler : this.uploadFile.createDelegate(this)
		}), new Ext.Action( {
			text : '关闭',
			disabled : false,
			handler : this.closeWin.createDelegate(this)
		})];
		this.uploadGrid = new Ext.grid.GridPanel( {
			autoExpandColumn : 2,
			enableHdMenu : false,
			buttonAlign :'center',
			width : 480,
			height : 260,
			tbar : [this.uploadAction[0], this.uploadAction[1],
					this.uploadAction[2]],
			bbar : [this.stateInfoBar],
			store : new Ext.data.SimpleStore( {
				fields : ["id", "state", "file", "size", "type"],
				data : []
			}),
			buttons:[ this.uploadAction[3],this.uploadAction[4] ], 
			columns : [new Ext.grid.RowNumberer(), {
				id : 'id',
				header : "id",
				hidden : true,
				width : 150,
				dataIndex : 'id',
				resizable : false,
				sortable : false
			}, {
				header : "文件名",
				width : Ext.grid.GridView.autoFill,
				dataIndex : 'file',
				sortable : true
			}, {
				header : "大小",
				width : 80,
				renderer : Ext.util.Format.fileSize,
				dataIndex : 'size',
				sortable : true,
				align : 'right'
			}, {
				header : "类型",
				width : 80,
				dataIndex : 'type',
				align : 'center',
				sortable : true
			}, {
				header : "状态",
				width : 100,
				dataIndex : 'state',
				align : 'center',
				sortable : true
			}]
		});		
		this.SWFUploadConfig = {			
			file_post_name : "Filedata",			
			file_types : "*.jpg;*.gif",
			file_types_description : "",
			file_size_limit : "1024",
			file_upload_limit : "10",
			file_queue_limit : "0",
			file_queued_handler : this.fileQueued.createDelegate(this),
			file_queue_error_handler : this.fileQueuedError.createDelegate(this),
			file_dialog_complete_handler : this.fileDialogComplete.createDelegate(this),
			upload_start_handler : this.uploadStart.createDelegate(this),
			upload_progress_handler : this.uploadProgress.createDelegate(this),
			upload_complete_handler : this.uploadComplete.createDelegate(this),
			upload_success_handler : this.uploadSuccess.createDelegate(this),
			upload_error_handler : this.uploadError.createDelegate(this),
			upload_url :this.upload_url,
			flash_url : this.flash_url,
			post_params : this.params,
			flash_width : "1px",
			flash_height : "1px",
			flash_color : "#FFFFFF",
			ui_container_id : "SWFUploadTarget",
			degraded_container_id : "divDegraded",
			
button_image_url: "../swfupload/XPButtonUploadText_61x22.png", // Relative to the Flash file
button_width: "61",
button_height: "22",
button_placeholder_id: "spanButtonPlaceHolder",
button_text: '<span class="x-btn-text"></span>',
button_text_style: ".theFont { font-size: 16;background-color:#00CCCC; }",
button_text_left_padding: 2,
button_text_top_padding: 4,			
			debug : true
		};
		
		ExtSWFUpload.superclass.constructor.call(this, {
			//layout : 'fit',
			width : 500,
			height : 340,
			title : this.title||'文件上传',
			closeAction : 'hide',
			border : false,
			modal : true,
			plain : true,
			closable : true,
			resizable : false,
			bbar : this.progressBar,
			items : [this.uploadGrid]
		});
		
		this.show();
		this.updateGridStaeInfoBar();
		

		//this.SWFUploadConfig.constructor.id=""+this.params_id;
		this.swfu = new SWFUpload(this.SWFUploadConfig);
		
		
	}
});